package com.xmg.client.mgrsite.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.service.ISkuPropertyService;
import com.xmg.shop.api.service.ISkuPropertyValueService;
import com.xmg.shop.api.util.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/skuPropertyValue")
public class SkuPropertyValueController {
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;
    @Reference
    private ISkuPropertyService skuPropertyService;

    @RequestMapping("/show/{skuPropertyId}")
    public String show(@PathVariable("skuPropertyId") Long skuPropertyId, Model model) {
        model.addAttribute("skuProperty", skuPropertyService.selectByskuPropertyId(skuPropertyId));
        model.addAttribute("skuPropertyValue", skuPropertyValueService.selectByskuPropertyId(skuPropertyId));
        return "sku/property_vale_list";
    }

    /**
     * 删除sku属性值
     *
     * @param skuPropertyValueId sku值的id
     * @return
     */
    @RequestMapping(value = "/deleteSkuValue/{skuPropertyValueId}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult deleteSkuValue(@PathVariable("skuPropertyValueId") Long skuPropertyValueId) {
        AjaxResult result = null;
        try {
            skuPropertyValueService.deleteSkuValue(skuPropertyValueId);
            result = new AjaxResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        }
        return result;
    }

    @RequestMapping("/add/{skuPropertyId}")
    @ResponseBody
    public AjaxResult add(@PathVariable("skuPropertyId") Long skuPropertyId, Long[] id, String[] value) {
        AjaxResult result = null;
        try {
            skuPropertyValueService.save(skuPropertyId, id, value);
            result = new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        }
        return result;
    }

}
